package com.fcl.blog.web;

import com.fcl.blog.comment.Result;
import com.fcl.blog.exception.ServiceException;
import com.fcl.blog.po.Tag;
import com.fcl.blog.service.TagService;
import com.fcl.blog.vo.Page;
import com.fcl.blog.vo.TagVo;
import com.fcl.blog.vo.TypeVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 付
 * @Date 2023/10/18 16:01
 * @Version 1.0
 * @description
 * @since 1.0
 */
@RestController
@RequestMapping("/admin")
@Slf4j
public class TagController {

    @Autowired
    private TagService service;

    @GetMapping("/tags")
    public Result getList(){
        PageHelper.startPage(1,7);
        List<Tag> listTag = service.findListTag();
        PageInfo<Tag> pageInfo = new PageInfo<>(listTag);
        return Result.success(pageInfo);
    }

    @GetMapping("/pageTag")
    public Result pageTags(@RequestParam("pageNum") String page){
        PageHelper.startPage(Integer.parseInt(page),new Page().getPagesize());
        List<Tag> listTag = service.findListTag();
        PageInfo<Tag> pageInfo = new PageInfo<>(listTag);
        return Result.success(pageInfo);
    }

    @PostMapping("/addTag")
    public Result addTag(@RequestBody TagVo tag) throws ServiceException {
        if (tag==null){
            log.error("抱歉，您输入的内容为空");
            return Result.error("抱歉，您输入的内容为空");
        }
        Tag one = service.selectByName(tag.getName());
        if (one!=null){
            log.error("抱歉，该标签已存在");
            return Result.error("抱歉，该标签已存在");
        }
        service.insertTag(tag);

        return Result.success("添加成功！");
    }

    @GetMapping("/deTag")
    public Result deleteType(@RequestParam("id") String id){
        if (id==null){
            return Result.error("抱歉，该id已经删除或者不存在");
        }
        int i = service.deleteById(Long.valueOf(id));
        if (i==0){
            return Result.error("删除失败！！");
        }
        return Result.success("修改成功");
    }

    //    修改Type
    @PostMapping("/upTag")
    public Result updateType(@RequestBody TagVo tagVo){
        if (tagVo==null){
            return Result.error("抱歉，未获取到数据，或者您输入的内容为空");
        }
        int i = service.updateTag(tagVo);
        if (i==0){
            return  Result.error("抱歉，修改失败！！");
        }
        return Result.success("修改成功");
    }


}
